package com.farsight.leo.fssmartlock.activities;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.farsight.leo.fssmartlock.Constants;
import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.bases.BaseActivity;
import com.farsight.leo.fssmartlock.bases.BaseApplication;
import com.farsight.leo.fssmartlock.net.OkHttpHelper;
import com.farsight.leo.fssmartlock.net.bases.BaseOkHttpCallback;
import com.farsight.leo.fssmartlock.net.bean.UserData;
import com.farsight.leo.fssmartlock.utils.ToastUtils;

import androidx.appcompat.widget.AppCompatEditText;

public class RegisterActivity extends BaseActivity {

    private AppCompatEditText mEdtUserName;
    private AppCompatEditText mEdtCellPhone;
    private AppCompatEditText mEdtEmail;
    private AppCompatEditText mEdtPw;
    private AppCompatEditText mEdtConfirmPw;

    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int set_layout_id() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mEdtCellPhone = findViewById(R.id.activity_register_edt_input_cell_phone);
        mEdtConfirmPw = findViewById(R.id.activity_register_edt_confirm_pw);
        mEdtEmail = findViewById(R.id.activity_register_edt_input_email);
        mEdtPw = findViewById(R.id.activity_register_edt_input_pw);
        mEdtUserName = findViewById(R.id.activity_register_edt_input_user_name);

    }

    @Override
    protected void initListeners() {

    }

    public void submit(View view) {
        String name = mEdtUserName.getText().toString().trim();
        final String pw = mEdtPw.getText().toString().trim();
        String pwConfirm = mEdtConfirmPw.getText().toString().trim();
        String email = mEdtEmail.getText().toString().trim();
        String phone = mEdtCellPhone.getText().toString().trim();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pw) || TextUtils.isEmpty(pwConfirm)
                || TextUtils.isEmpty(email) || TextUtils.isEmpty(phone)) {
            return;
        }
        if (!pw.equals(pwConfirm)) {
            return;
        }
        UserData data = new UserData();
        data.setUser_name(name);
        data.setEmail_address(email);
        data.setTelephone_number(phone);
        data.setUser_password(pw);

        OkHttpHelper.getInstance().requestUserRegistration(data, new BaseOkHttpCallback<UserData>() {
            @Override
            public void onError(final String err) {
                ToastUtils.showToast(err);
            }

            @Override
            public void onSuccess(final UserData result) {
                result.setUser_password(pw);
                Intent starter = new Intent(RegisterActivity.this, PostLoginActivity.class);
                starter.putExtra(Constants.KEY_USER_DATA, result);
                starter.putExtra(Constants.KEY_INTENT_SOURCE,Constants.INTENT_REGISTER_OK );
                startActivity(starter);
                finish();
            }
        });


    }
}
